/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import bean.entity.Employees;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Vu Thang
 */
@Stateless
@LocalBean
public class EmployeeSessionBean {

    @PersistenceContext(unitName = "RecruitmentProcessSystem-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public Employees checkLogin(String emp_no, String password) {
        Employees emp = null;
        try {
            Query query = em.createNamedQuery("Employees.findByEmpNoAndPassword");
            query.setParameter("empNo", emp_no);
            query.setParameter("password", password);
            emp = (Employees) query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        return emp;
    }

    public Employees updateEmployes(Employees emp) {
        return em.merge(emp);
    }

    public int changePassEmployes(String id, String pass) {
        int i = 0;
        try {
            Query query = em.createNamedQuery("Employees.changePass");
            query.setParameter("password", pass);
            query.setParameter("empNo", id);
            i = query.executeUpdate();
        } catch (Exception e) {
            return 0;
        }
        return i;
    }

    public List<Employees> findEmployeeByDepartID(String departmentID) {
        List<Employees> emps = null;
        try {
            Query query = em.createNamedQuery("Employees.findByDepartID");
            query.setParameter("departmentID", departmentID);
            emps = query.getResultList();
            return emps;
        } catch (NoResultException e) {
            return null;
        }
    }

}


